package com.stadium.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.stadium.common.exception.RRException;
import com.stadium.common.utils.SqlMapper;
import com.stadium.common.utils.UserUtils;
import com.stadium.dto.GameTeamDto;
import com.stadium.dto.REventTeamDto;
import com.stadium.dto.TeamListDto;
import com.stadium.entity.REventTeam;
import com.stadium.mapper.GameTeamMP;
import com.stadium.mapper.REventTeamMP;
import com.stadium.service.REventTeamSV;
import com.stadium.vo.REventTeamVo;
import com.stadium.vo.searchVo.REventTeamSearchVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class REventTeamSVI implements REventTeamSV {
    @Autowired
    private REventTeamMP mapper;
    @Autowired
    private SqlMapper sqlMapper;
    @Autowired
    private GameTeamMP gameTeamMP;

    @Override
    public List<REventTeam> queryBySQL(String sql, Map<String, Object> map) {
        return sqlMapper.selectList(sql, map, REventTeam. class);
    }

    @Override
    public void save(REventTeamVo rEventTeamVo) {
        REventTeam rEventTeam =new  REventTeam();
        BeanUtils.copyProperties(rEventTeamVo,rEventTeam);
        String id = UUID.randomUUID().toString().replaceAll("-", "");
                                                                                                                                                                                                                                                                                                                                                rEventTeam.setId(id);
        mapper.save(rEventTeam);
    }

    @Override
    public void update(REventTeamVo rEventTeamVo) {
        REventTeam rEventTeam =new  REventTeam();
        BeanUtils.copyProperties(rEventTeamVo,rEventTeam);
        mapper.update(rEventTeam);
    }

    @Override
    public void saveBatch(List<REventTeam> list) {
        mapper.saveBatch(list);
    }

    @Override
    public void delete(String id) {
        mapper.delete(id);
    }

    @Override
    public void deleteBatch(String[] ids) {
        mapper.deleteBatch(ids);
    }


    @Override
    public List<REventTeamDto> query(REventTeamSearchVo searchVo) {
        return mapper.query(searchVo);
    }

    @Override
    public REventTeamDto get(String id) {
        return mapper.get(id);
    }

    @Override
    public int getCount(REventTeamSearchVo searchVo) {
        return mapper.getCount(searchVo);
    }

    @Override
    public List<TeamListDto> queryTeamByEventId(String id) {
        return mapper.queryTeamByEventId(id);
    }

    @Override
    public void saveTeams(String eventId, String[] teamIds) {
        for(String teamId : teamIds){
            Integer i = mapper.queryTeamByEventIdAndTeamId(eventId,teamId);
            if(i > 0){
                GameTeamDto gameTeamDto = gameTeamMP.get(teamId);
                throw new RRException("球队"+gameTeamDto.getName()+"已加入，请勿重复加入");
            }
            REventTeam rEventTeam = new REventTeam();
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            rEventTeam.setId(id);
            rEventTeam.setTeamId(teamId);
            rEventTeam.setEventId(eventId);
            rEventTeam.setAuditStatus("1");
            mapper.save(rEventTeam);
        }

    }

    @Override
    public void saveTeamApply(String eventId, String[] teamIds) {
        for(String teamId : teamIds){
            Integer i = mapper.queryTeamByEventIdAndTeamId(eventId,teamId);
            if(i > 0){
                GameTeamDto gameTeamDto = gameTeamMP.get(teamId);
                throw new RRException("球队"+gameTeamDto.getName()+"已加入，请勿重复加入");
            }
            REventTeam rEventTeam = new REventTeam();
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            rEventTeam.setId(id);
            rEventTeam.setTeamId(teamId);
            rEventTeam.setEventId(eventId);
            rEventTeam.setAuditStatus("2");
            mapper.save(rEventTeam);
        }

    }

}
